package com.ruoyi.douyin.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.douyin.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/base")
public class AuthController {

    @Autowired
    private AuthService authService;

    /**
     * 抖音授权
     */
    @PostMapping("douyin/code")
    public AjaxResult douyinAuth() {
        String path = authService.douyinAuth();
        return AjaxResult.success("redirect_uri", path);
    }

    /**
     * 通过code获取accessToken
     *
     * @param code 授权码
     */
    @GetMapping("douyin/access/token")
    public void douyinAccessToken(@RequestParam String code,
                                  @RequestParam String state,
                                  HttpServletResponse response) {
        AjaxResult result = authService.douyinAccessToken(code, state);
        try {
            if (result.get("msg").toString().equalsIgnoreCase("success")) {
                response.sendRedirect("http://zs.91gongju.cn/");
            } else {
                response.sendRedirect("http://zs.91gongju.cn");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // return new ModelAndView("redirect:http://zs.91gongju.cn");
    }

//    @GetMapping("douyin/bind/users")
//    public AjaxResult bindUsers() {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        return douyinUserService.bindUsers(loginUser.getUser().getUserId());
//    }

    @PostMapping("douyin/info")
    public AjaxResult userInfo(@RequestBody List<String> originOpenId) {
        return authService.userInfo(originOpenId);
    }

    @GetMapping("douyin/bind/info/list")
    public AjaxResult userInfoList() {
        return authService.userInfoList();
    }

    @GetMapping("douyin/bind/info/list/full")
    public AjaxResult userInfoListFull() {
        return authService.userInfoListFull();
    }
}
